/**
 * 2013 2013-5-9
 * @author asfklhaskjfgh
 */
package com.surfilter.dp.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import com.surfilter.dp.constant.Constants;
import com.surfilter.dp.context.ContextHolder;
import com.surfilter.dp.model.ActionModel;
import com.surfilter.dp.model.ModuleModel;
import javax.swing.JSeparator;

/**
 * @author asfklhaskjfgh
 * 
 */
public class ActionPanel extends JPanel {
	private JButton saveBtn;
	private JButton addBtn;
	private JButton delBtn;
	private JTable table;
	private DefaultTableModel tableModel;
	
	private int eRow = -1;
	private int eColumn = -1;
	
	private  ModuleModel boforeSelect;
	private  ModuleModel afterSelect;
	
	public ActionPanel() {
		super();
		initView();
		initListeners();
	}

	/**
	 * 
	 */
	private void initListeners() {
		final ActionPanel panel = this;
		final ContextHolder context = ContextHolder.getInstance();

		final DefaultCellEditor editor =  (DefaultCellEditor)table.getDefaultEditor(String.class);
		final JTextField textfield = (JTextField)editor.getComponent();
		textfield.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				editor.cancelCellEditing();
				String newVal = textfield.getText();
				afterSelect = context.getModuleTree().getSelectedModule();
				if(eRow != -1 && eColumn != -1){
					if(afterSelect.equals(boforeSelect)){
						tableModel.setValueAt(newVal, eRow, eColumn);
						table.setModel(tableModel);
					}
				}
			}
			@Override
			public void focusGained(FocusEvent e) {
				eRow = table.getEditingRow();
				eColumn = table.getEditingColumn();
				boforeSelect = context.getModuleTree().getSelectedModule();
			}
		});
		saveBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				List<ActionModel> actions = panel.getTableData();
				if (actions.size() > 0) {
					context.getActionDao().batchUpdateActions(actions);
					JOptionPane.showMessageDialog(context.getMainWin(), "修改action成功");
				}
			}
		});
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModuleModel module = context.getModuleTree().getSelectedModule();
				if(module != null && module.getModuleType() == Constants.TYPE_FUN){
					int moduleId = module.getId();
					if(moduleId == 0){
						module = context.getModuleDao().getModuleByName(module.getModuleName());
						context.getModuleTree().setSelectedModule(module);
					}
					new ActionAddDialog(context.getMainWin()).setVisible(true);
				}else{
					JOptionPane.showMessageDialog(context.getMainWin(), "选中模块无法添加action");
				}
			}
		});
		delBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				List<ActionModel> selectedActions= getSelectedActions();
				if(selectedActions.size() > 0){
					context.getActionDao().deleteActions(selectedActions);
					ModuleModel module = context.getModuleTree().getSelectedModule();
					List<ActionModel> actions = context.getActionDao().getActionByModuleId(module.getId());
					setTableData(actions);
					JOptionPane.showMessageDialog(context.getMainWin(), "删除成功!");
				}else{
					JOptionPane.showMessageDialog(context.getMainWin(), "请至少选中一个action");
				}
			}
		});
	}
	/**
	 * 
	 */
	private void initView() {
		this.setBackground(Color.WHITE);
		this.setLayout(new BorderLayout(0, 0));
		JToolBar toolBar = new JToolBar();
		this.add(toolBar, BorderLayout.NORTH);

		saveBtn = new JButton("保存修改");
		saveBtn.setHorizontalAlignment(SwingConstants.LEADING);
		toolBar.add(saveBtn);

		addBtn = new JButton("添加action");
		addBtn.setHorizontalAlignment(SwingConstants.LEADING);
		toolBar.add(addBtn);

		delBtn = new JButton("删除action");
		delBtn.setHorizontalAlignment(SwingConstants.LEADING);
		toolBar.add(delBtn);

		tableModel = new DefaultTableModel(null, new String[] { "ID",
				"aciton名称", "action描述" });

		table = new JTable(tableModel);
		table.setRowHeight(40);
		table.setRowSelectionAllowed(true);
		JScrollPane jsp = new JScrollPane();
		jsp.setViewportView(table);
		this.add(jsp, BorderLayout.CENTER);
		
	}
	
	public void setTableData(List<ActionModel> actions) {
		int count = this.tableModel.getRowCount();
		for (int i = 0; i < count; i++) {
			tableModel.removeRow(0);
		}
		for (ActionModel actionModel : actions) {
			this.tableModel.addRow(new Object[] { actionModel.getId(),
					actionModel.getActionName(), actionModel.getActionText() });
		}
	}

	public List<ActionModel> getTableData() {
		List<ActionModel> actions = new ArrayList<ActionModel>();
		for (int i = 0; i < this.tableModel.getRowCount(); i++) {
			
			
			ActionModel action = new ActionModel();
			action.setId(Integer
					.valueOf(table.getValueAt(i, 0).toString()));
			action.setActionName(table.getValueAt(i, 1).toString());
			action.setActionText(table.getValueAt(i, 2).toString());
			actions.add(action);
			
		}
		return actions;
	}
	
	public List<ActionModel> getSelectedActions(){
		List<ActionModel> actions = new ArrayList<ActionModel>();
		int[] rows = table.getSelectedRows();
		for (int i : rows) {
			ActionModel action = new ActionModel();
			action.setId(Integer
					.valueOf(tableModel.getValueAt(i, 0).toString()));
			action.setActionName(tableModel.getValueAt(i, 1).toString());
			action.setActionText(tableModel.getValueAt(i, 2).toString());
			actions.add(action);
		}
		return actions;
	}
	
}
